<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\TaskModel;
use common\service\StepSkuService;
use Webman\Stomp\RetryAbleConsumer;

/**
 * Summary of CalculateTaskManHourConsumer
 */
class CalculateTaskManHourConsumer extends BaseConsumer implements RetryAbleConsumer
{
    // 要消费的队列名
    /**
     * Summary of queue
     * @var string
     */
    public $queue = 'calculate_task_man_hour';

    // 连接名，对应 config/redis_queue.php 里的连接`
    /**
     * Summary of connection
     * @var string
     */
    public $connection = 'default';

    /**
     * Summary of doConsume
     * @param mixed $data
     * @return void
     */
    public function doConsume($data)
    {
        $task = model(TaskModel::class)->find($data['task_id']);
        if (empty($task)) {
            return ;
        }
        if (!in_array($task['step_type'], [TaskModel::STEP_TYPE_STEP, TaskModel::STEP_TYPE_STEP_CATEGORY])) {
            return;
        }
        StepSkuService::getInstance()->calculateTaskManHour($data['task_id']);

    }

    /**
     * Summary of getTtr
     * @return int
     */
    public function getTtr()
    {
        return 20;
    }

    /**
     * Summary of canRetry
     * @param mixed $attempt
     * @param mixed $error
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        trace(format_exception_info($error), 'ERR');
        return $attempt <= 3;
    }
}
